package com.zyy.ui.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: LogUtil
 * @Description: 日志工具类
 * @Author: zyy
 * @Date: 2020/04/26 20:30
 * @Version: 1.0
 */
public class LogUtil {
    private static final int STACK_DEPTH = 3;
    private static final String TAB = "\t";
    private static Logger logger = LoggerFactory.getLogger("logger");

    public static void info(Object... message) {
        //message添加额外信息（格式化message）
        logger.info(getMessage(LogJsonSerialization.getInstance().toJSONString(message)));
    }

    public static void error(Throwable ex, Object... message) {
        //message添加额外信息（格式化message）
        logger.error(getMessage(LogJsonSerialization.getInstance().toJSONString(message)), ex);
    }

    private static String getMessage(String message) {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        if (stackTraceElements.length > STACK_DEPTH && stackTraceElements[STACK_DEPTH] != null) {
            message = stackTraceElements[STACK_DEPTH].getClassName()
                    + TAB
                    + stackTraceElements[STACK_DEPTH].getMethodName()
                    + TAB
                    + message;
        }
        return message;
    }

}
